package sziit.znk.beans;

import sziit.znk.utils.JDBCPropertiesFilesLoader;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author znk
 * @time 2020-06-30
 * 释放connection：
 */
public class DataSource {

    private String url;
    private String user;
    private String password;
    private String driverClass;

    private Connection connection;
    private JDBCPropertiesFilesLoader jdbcPropertiesFilesLoader;

    public Connection getConnection() {
        try {
            Class.forName(driverClass);
            connection = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public DataSource() {
    }

    public DataSource(String name) {
        jdbcPropertiesFilesLoader = new JDBCPropertiesFilesLoader(name);
        driverClass = jdbcPropertiesFilesLoader.getDRIVER_CLASS();
        url = jdbcPropertiesFilesLoader.getURL();
        user = jdbcPropertiesFilesLoader.getUSER();
        password = jdbcPropertiesFilesLoader.getPASSWORD();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClass() {
        return driverClass;
    }

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    @Override
    public String toString() {
        return "DataSource{" +
                "url='" + url + '\'' +
                ", user='" + user + '\'' +
                ", password='" + password + '\'' +
                ", driverClass='" + driverClass + '\'' +
                '}';
    }

}
